https://leetcode.cn/problems/bLyHh0/
class Solution {
public:
    int findCircleNum(vector<vector<int>>& isConnected) {
        int n=isConnected[0].size();
        vector<int> _ufs(n,-1);
        auto FindRoot=[&_ufs](int x)
        {
            int parent=x;
            while(_ufs[parent]>=0)
            {
                parent=_ufs[parent];
            }
            return parent;
        };

        for(int i=0;i<n;i++)
        {
            for(int j=0;j<=i;j++)
            {
                if(isConnected[i][j]==1)
                {
                    int root1=FindRoot(i);
                    int root2=FindRoot(j);
                    if(root1!=root2)
                    {
                        _ufs[root1]+=_ufs[root2];
                        _ufs[root2]=root1;
                    }
                }
            }
        }

        int count =0;
        for(auto x:_ufs)
        {
            if(x<0)
                count++;
        }
        return count;


    }
};